﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace 验证码识别
{
    public partial class mainForm : Form
    {
        string sAccessConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\config.mdb;"; //数据库链接
        int iCharTotal = 0;
        int iCorrect = 0;
        public mainForm()
        {
            InitializeComponent();
        }
        private void btGetVerify_Click(object sender, EventArgs e)
        {
            //建立Web请求获取图像数据流
            HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(tbVerifyURL.Text);
            wreq.KeepAlive = true;
            HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();
            Stream s = wresp.GetResponseStream();
            //转换为图形
            Bitmap Img = new Bitmap(Image.FromStream(s));
            //设置显示的图片大小和位置
            pSrc.Width = Img.Width;
            pSrc.Height = Img.Height;
            pSrc.Top= btGetVerify.Bottom-Img.Height;
            pSrc.Image = Img;
            lbImageSize.Text= Img.Width.ToString() + "X" + Img.Height.ToString();
        }

        private void btToGrey_Click(object sender, EventArgs e)
        {
            
            pToGrey.Width = pSrc.Width;
            pToGrey.Height = pSrc.Height;
            pToGrey.Top = btToGrey.Bottom - pToGrey.Height;
            pToGrey.Image = VerifyCode.toGrey((Bitmap)pSrc.Image, Convert.ToInt32(udGreyThreshold.Value));           
        }

        private void btDeleteBorder_Click(object sender, EventArgs e)
        {
            /*去噪点
            pBlackClear.Width = pToGrey.Width + Convert.ToInt32(udPointWidth.Value)*2;
            pBlackClear.Height = pToGrey.Height + Convert.ToInt32(udPointWidth.Value)*2;
            pBlackClear.Top = btBlackClear.Bottom - pBlackClear.Height;
            pBlackClear.Image = VerifyCode.blackClear((Bitmap)pToGrey.Image,Convert.ToInt32(udPointWidth.Value), Convert.ToInt32(udBlackClearThreshold.Value));
        */
            pBlackClear.Image = VerifyCode.deleteBorder((Bitmap)pToGrey.Image, Convert.ToInt32(udBorderWidth.Value));

        }

        private void btImageSplit_Click(object sender, EventArgs e)
        {
            Bitmap Img = new Bitmap(pictureBox.Width, pictureBox.Height);
            int [] iPointCounts= VerifyCode.pointShow(ref Img, (Bitmap)pBlackClear.Image);
            pictureBox.Image = Img; //图形显示
            string sPointCount = "";
            for (int i = 0; i < iPointCounts.Length; i++)
            {
                sPointCount += iPointCounts[i].ToString() + ",";
            }
            //显示字符串
            tbPointCount.Text = sPointCount.TrimEnd(',');
            //切割完成
            ClipImage clipImage = VerifyCode.imageSplit((Bitmap)pBlackClear.Image,(int)udMinWidth.Value,(int)udSplitThreshold.Value);
            pImageSplit.Image=clipImage.ImagePreView;
            for(int i=0;i<8;i++){
                PictureBox picturebox = (PictureBox)this.Controls["pVerify" + i.ToString()];
                TextBox textbox=(TextBox)this.Controls["tbVerify"+i.ToString()];
                Label labelSize = (Label)this.Controls["lbSize" + i.ToString()];
                if (clipImage.ClipImages[i] != null)
                {
                    picturebox.Height = clipImage.ClipImages[i].Height;
                    picturebox.Width = clipImage.ClipImages[i].Width;
                    picturebox.Image = clipImage.ClipImages[i];
                    labelSize.Text = picturebox.Width + "X" + picturebox.Height;
                }
            }
        }


        private void btVerifyCode_Click(object sender, EventArgs e)
        {
            btToGrey_Click(null, null);
            btDeleteBorder_Click(null, null);
            btFireSplit_Click(null, null);
            btImageResize_Click(null, null);
            btGetCode_Click(null, null);
        }
        private void btSaveImage_Click(object sender, EventArgs e)
        {
            Button button=(Button)sender;
            string sOrder = button.Name.Replace("btSave", "");
            PictureBox picturebox = (PictureBox)this.Controls["pResize" + sOrder];
            TextBox textbox = (TextBox)this.Controls["tbVerify" + sOrder];
            if (picturebox.Image != null)
            {
                VerifyCode.saveImage((Bitmap)picturebox.Image, textbox.Text, sAccessConnStr, tbVerifyURL.Text);
            }
        }
        /// <summary>
        /// 验证码来源改变以后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbVerifyURL_TextChanged(object sender, EventArgs e)
        {
            switch (tbVerifyURL.Text)
            {
                case "http://www.uuplay.com/ValidateCode":
                    udGreyThreshold.Value = 160;
            
                    udBorderWidth.Value = 1;
                    udSplitThreshold.Value = 1;
                    udMinWidth.Value = 3;
                    break;
                case "http://user.ly.kunlun.com/Verify/login":
                    udGreyThreshold.Value = 112;
                    udBorderWidth.Value = 1;
                    udSplitThreshold.Value = 1;
                    udMinWidth.Value = 8;
                    break;
                case "http://loginserver.ourgame.com/login/OGContorl/heart.ashx":
                    udGreyThreshold.Value = 240;
                    udBorderWidth.Value = 1;
                    udSplitThreshold.Value = 1;
                    udMinWidth.Value = 8;
                    break;
                default:
                    break;
            }
        }

        private void btImageResize_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 8; i++)
            {
                PictureBox picturebox = (PictureBox)this.Controls["pVerify" + i.ToString()];
                PictureBox picturebox2 = (PictureBox)this.Controls["pResize" + i.ToString()];
                if(picturebox.Image!=null)
                    picturebox2.Image = VerifyCode.reSizeImage((Bitmap)picturebox.Image);
            }
        }

        private void btGetCode_Click(object sender, EventArgs e)
        {
            PictureBox picturebox;
            Label labelLike;
            TextBox textbox;
            for (int i = 0; i < 8; i++)
            {
                picturebox = (PictureBox)this.Controls["pResize" + i.ToString()];
                labelLike = (Label)this.Controls["lbLike" + i.ToString()];
                textbox = (TextBox)this.Controls["tbVerify" + i.ToString()];
                if (picturebox.Image != null)
                {
                    VerifyResult verifyResult = VerifyCode.searchCode((Bitmap)picturebox.Image, sAccessConnStr, tbVerifyURL.Text);
                    textbox.Text = verifyResult.Code;
                    iCharTotal++;
                    if (verifyResult.Code != "*")
                        iCorrect++;
                    btClearSummary.Text=iCorrect.ToString()+"/"+iCharTotal.ToString()+","+((iCorrect*100)/iCharTotal).ToString()+"%";
                    labelLike.Text = (Convert.ToInt32(verifyResult.Likeness * 100) / 100.0).ToString();
                }
            }
        }

        private void tbClearSummary_Click(object sender, EventArgs e)
        {
            iCharTotal = 0;
            iCorrect = 0;
            btClearSummary.Text = "已重置";
        }

        private void btFireSplit_Click(object sender, EventArgs e)
        {
            ClipImage clipImage = VerifyCode.imageSplit((Bitmap)pBlackClear.Image,Convert.ToInt32(udUsedPercent.Value));
            pImageSplit.Image = clipImage.ImagePreView;
            for (int i = 0; i < 8; i++)
            {
                PictureBox picturebox = (PictureBox)this.Controls["pVerify" + i.ToString()];
                TextBox textbox = (TextBox)this.Controls["tbVerify" + i.ToString()];
                Label labelSize = (Label)this.Controls["lbSize" + i.ToString()];
                if (clipImage.ClipImages[i] != null)
                {
                    picturebox.Height = clipImage.ClipImages[i].Height;
                    picturebox.Width = clipImage.ClipImages[i].Width;
                    picturebox.Image = clipImage.ClipImages[i];
                }
            }
        }
    }
}
